$(function () {
  var page = 1
  var pageSize = 5
  render()

  function render() {
    $.ajax({
      url: '/category/queryTopCategoryPaging',
      type: 'get',
      data: {
        page: page,
        pageSize: pageSize
      },
      success: function (info) {
        console.log(info)
        $('tbody').html(template('tmp', info));
        $('#paginator').bootstrapPaginator({
          bootstrapMajorVersion: 3,
          currentPage: page,
          totalPages: Math.ceil(info.total / info.size),
          onPageClicked: function (a, b, c, p) {
            page = p
            render()
          }
        })
      }
    })
  }

  $('.addCate').on('click', function () {
    $('#addModal').modal('show')
  })

  $('form').bootstrapValidator({
    feedbackIcons: {
      valid: 'glyphicon glyphicon-thumbs-up',
      invalid: 'glyphicon glyphicon-thumbs-down',
      validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
      categoryName: {
        validators: {
          notEmpty: {
            message: '一级分类的名称不能为空'
          }
        }
      }
    },
  })

  $('form').on('success.form.bv',function(e) {
    e.preventDefault();
    $.ajax({
      url: '/category/addTopCategory',
      type: 'post',
      data: $('form').serialize(),
      success: function(info) {
        if(info.success) {
          page = 1
          render()
          $('#addModal').modal('hide')
          $('form').data('bootstrapValidator').resetForm(true)
        }
      }
    })
  })
})